/*
 * =====================================================================================
 *
 *       Filename:  blockQueue.h
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  06/26/2012 04:00:45 PM
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */

#ifndef BLOCKQUEUE_H
#define BLOCKQUEUE_H

#include"condition.h"
#include<boost/noncopyable.hpp>
#include<deque>
namespace inm
{
	namespace concurrency
	{
		//BlockQueue can not be defined in blcokQueue.cpp 
		//because template class may not be complied in different if it is divided into declaration(in xx.h) and defination( in xx.cpp) 
		template<typename T>
		class BlockQueue
		{
			public:
				BlockQueue() : condition_(mutex_) 
				{}
				void pushQueue(const T& element)
				{
					MutexLockGuard lock(mutex_);
					queue_.push_back(element);
					condition_.notify();
				}
				T popQueue()
				{
					MutexLockGuard lock(mutex_);
					while(queue_.empty())
					{
						condition_.wait();
					}
					assert(!queue_.empty());
					T element(queue_.front());
					queue_.pop_front();//pop_frot just pop one element,not have return value
					return element;
				}
				int size() const
				{
					MutexLockGuard lock(mutex_);	
					return queue_.size();
				}
			private:
				std::deque<T> queue_;
				mutable MutexLock mutex_;
				Condition condition_;
		};
	};
};

#endif //BLOCKQUEUE_H
